*WHAT'S ACROSS THE BORDER?
*JHA, NEUMARK, AND RODRIGUEZ-LOPEZ

include directories.do

set level 90, perm

***DLR PAIR REGRESSIONS

******** CONVENTIONAL TWFE

foreach d in "emp" {

* Load data at the czone-state-industry level
use "`datadir'`s'cbp_county_state_20ind_sample.dta", clear

*Remove DC (starts in 2004)
drop if czone==11304 & state==11

*Keep restaurant industry
keep if wagerank90==1

*egen czonestate=group(czone state)

* Generating variables 

gen mw=max_mw
gen lmw=log(mw)

*Generate totempm, which removes emp of the industry of interest
foreach b in "emp" {
gen tot`b'm=tot`b'-`b'
}

foreach b in "emp" "wageap" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
gen ltot`b'm=log(tot`b'm)
}

gen lworkagepop=log(workagepop)

**Generate YEAR DUMMIES 
qui tab year, gen(yeard)

 *Dummies region-year
qui tab region, gen(regiond)
gen regionyear=region*10000+year
qui tab regionyear, gen(regionyeard)

*State dummies
qui tab state, gen(stated)
gen trend=year-1989
forvalues z = 1/48 {
gen statedtrend`z'=stated`z'*trend
}

tsset county year

* Leads and lags

forval j = 1(1)4 {
 
	gen lmw_l`j' =  L`j'.lmw
       
      gen lmw_f`j' = F`j'.lmw
     
} 

gen lmw_l0 = lmw
local uf 4
local mf 3
local lf 1
gen lmw_f`uf'_`mf'=lmw_f`uf'-lmw_f`mf'
gen lmw_f`mf'_`lf'=lmw_f`mf'-lmw_f`lf'
gen lmw_f`lf'_0=lmw_f`lf'-lmw

*Dependent variable and regressors (SAME FOR COUNTY OR CZONE)

local mwvar lmw
local popvar lworkagepop
local wagevar lwageap
local empvar lemp
local totempvar ltotempm
local totwagevar ltotwageapm

**************generating conditions and controls*************
local window1 "year<=2016"

* Specify clustering

local options "vce(cluster state)  absorb(county year)"


* Specify outcome/regressor/instrument
local xvar "lmw"

* Specify controls
local year_controls yeard*
local regionyear_controls regionyeard*
local statetrend_controls statedtrend*


ren cty_fips county
merge m:1 county using "`countydir'`s'countytypes.dta"
drop _merge

/* Regression specs */

foreach j in "1" "2" "3" "4" {
foreach h in "emp" {
local yvar "``h'var'"
local controls "`tot`h'var' `popvar'"
local pretrends "lmw_f`mf'_`lf' lmw_f`lf'_0 lmw"
local pretrends2 "lmw_f`mf' lmw_f`lf' lmw"

eststo m`j'1_`h': reghdfe `yvar' `pretrends' `controls' if type`j'==1, `options'
eststo m`j'2_`h': reghdfe `yvar' `pretrends2' `controls' if type`j'==1, `options'



}
}

}

******* PAIR-PERIOD FIXED EFFECTS
foreach u in "county" {
* Load stacked pair data at the county or czone-state level
use "`datadir'`s'cbp_stacked_`u'pair_sample.dta", clear

************************************************
*******COUNTY-PAIRS OR MULTI-STATA CZONE PAIRS
************************************************

*** PAIR ID VARIABLE (EITHER CZONESTATEPAIR OR COUNTYPAIR)
local pair `u'pair


*Entity variable (EITHER CZONESTATE OR COUNTY)
local geo `u'



***********************************************
*GENERATE VARIABLES (SAME FOR COUNTY OR CZONE)
***********************************************

egen idvar=group(`u'pair state)

gen lmw=log(mw)

tsset idvar year

* Leads and lags

forval j = 1(1)4 {
 
	gen lmw_l`j' =  L`j'.lmw
       
      gen lmw_f`j' = F`j'.lmw
     
} 

gen lmw_l0 = lmw
local uf 4
local mf 3
local lf 1
gen lmw_f`uf'_`mf'=lmw_f`uf'-lmw_f`mf'
gen lmw_f`mf'_`lf'=lmw_f`mf'-lmw_f`lf'
gen lmw_f`lf'_0=lmw_f`lf'-lmw



*Generate totestm and totempm, which remove the est or emp of the industry of interest
gen totestm=totest-est
gen totempm=totemp-emp

foreach b in "emp" "wageap" "wageqp" "est" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
}

gen ltotwageapm=log(totwageapm)
gen ltotwageqpm=log(totwageqpm)
gen ltotestm=log(totestm)
gen ltotempm=log(totempm)
gen ltotpop=log(totpop)
gen lworkagepop=log(workagepop)


********************************************
**** DEFINE LOCAL VARIABLES
********************************************


local period year

*Dependent variable and regressors (SAME FOR COUNTY OR CZONE)

local popvar lworkagepop
local totempvar ltotempm
local totwagevar ltotwageapm
local wagevar lwageap
local empvar lemp

********* MERGE PAIRS ********************
drop state
merge m:1 countypair_id county using "`countydir'`s'countypairtypes.dta"

drop _merge

gen state=int(county/1000)
bys countypair_id: egen state1=min(state)
bys countypair_id: egen state2=max(state)

**************************************************
****STACKED REGRESSIONS WITH REGHDFE
**************************************************

**************************************************
****STACKED REGRESSIONS WITH REGHDFE
**************************************************

sort  `pair' `period'

egen bordersegment = group(state1 state2)
egen pairperiod=group(`u'pair `period')

*USE DATA UP TO 
drop if `period'>2016

*Specify clustering
local options "vce(cluster bordersegment state) absorb(`u' pairperiod)"

*Specifications

forval j=1/4 {

foreach h in "emp" {

local yvar "``h'var'"
local controls " `popvar'"
local controls2 "`tot`h'var' `popvar'"
local pretrends "lmw_f`mf'_`lf' lmw_f`lf'_0 lmw"
local pretrends2 "lmw_f`mf' lmw_f`lf' lmw"



eststo n1`j'_`h': reghdfe `yvar' `pretrends' `controls2' if type`j'==1, `options'
eststo n2`j'_`h': reghdfe `yvar' `pretrends2' `controls2' if type`j'==1, `options'


}
}

*WITH LEADS-LAGS WE LOSE TWO PAIRS AND ONE COUNTY ON COL 1 (841 PAIRS AND 928 COUNTIES)

}


/* Print results */

tempfile specs

#delimit ;
esttab m* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Pretrends -- CONVENTIONAL TWFE")
	order(`pretrends' `pretrends2')
	nonotes
	label
	stats(N, labels("N") fmt(0))	
	replace;
#delimit cr

#delimit ;
esttab n* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Pretrends -- PAIR-PERIOD FE")
	order(`pretrends' `pretrends2')
	nonotes
	label
	stats(N, labels("N") fmt(0))	
	append;
#delimit cr

* Switch the output to Excel format
insheet using "`specs'.csv", comma clear
export excel "`outdir'`s'TablesJNR.xlsx", sheetreplace sheet(Table10)

clear all


